import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Layout from '../views/layout/index.vue'
import Articles from '../views/article/index.vue'
import AddArticle from '../views/article/add.vue'
// import 404 from '../views/404/index.vue'
Vue.use(VueRouter)
    // const routes = [{path:'/',name:'Home',component:Home},{path:'/about',name:'About',component:() => import('../views/About.bue')}]
    // const routes = new VueRouter({
    //   routes
    // })
    // export default router

const routes = [{
    path: '/',
    name: 'layout',
    component: Layout,
    children: [{
        path: '/articles',
        component: () =>
            import ('../views/article')
        },
    {
        path: '/login',
        name: 'login',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/login')
    },
    {
        path: '*',
        name: 'page404',
        component: () =>
            import ('../views/page404')
    }, {
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/About.vue')
    },{   
        path: '/articles',
        name: 'Articles',
        component:Articles
    },{
        path: '/addArticles',
        name: 'AddArticles',
        component:AddArticles
    },{
    //动态路由
        path: '/editArticle/:id',
        name: 'EditArticle',
        component:EditArticle
    },{
    //素材管理
        path:'image',
        name:'Image',
        compont:Image
    },
    //添加文章
    {
        path:'/addArticle',
        name:'AddArticle',
        compont:AddArticle
    }
    ]
  }
]

const router = new VueRouter ({
        routes
    })
    //to跳转
    //from从哪来
    //next 放行方法
    //  next():正常跳转
    //  next('/XXX'):跳转到其它
router.beforeEach ((to, from, next) => {
    console.log(to, from, next)
    if (to.path === '/') {
        //检查是否有token
        const token = localStorage.getItem('tokenStr')
        if (token) {
            next()
        } else {
            next('/login')
        }
    } else {
        next()
    }
})

    export default router